package com.zt.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zt.mapper.SetmealMapper;
import com.zt.pojo.Setmeal;
import com.zt.pojo.vo.SetmealVo;
import com.zt.service.SetmealService;
import com.zt.util.RedisClient;
import com.zt.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 不.凌
* @description 针对表【t_setmeal】的数据库操作Service实现
* @createDate 2024-11-17 19:36:06
*/
@Service
public class SetmealServiceImpl implements SetmealService{
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private RedisClient redisClient;

    /**
     * 分页模糊查询
     * @param setmealVo
     * @return
     */
    @Override
    public Result findPage(SetmealVo setmealVo) {
        PageHelper.startPage(setmealVo.getCurrentPage(), setmealVo.getPageSize());
        List<Setmeal> setmealList = setmealMapper.findPage(setmealVo);
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);
        return Result.success("查询成功",pageInfo);
    }

    /**
     * 新增套餐
     * @param checkgroupIds
     * @param setmeal
     */
    @Override
    public void add(Integer[] checkgroupIds, Setmeal setmeal) {
        setmealMapper.add(setmeal);
        setmealMapper.addSetmealCheckgroup(checkgroupIds,setmeal.getId());
    }

    /**
     * 删除套餐
     * @param id
     */
    @Override
    public void delete(Integer id) {
        setmealMapper.deleteSetmealCheckgroup(id);
        setmealMapper.delete(id);
    }

    /**
     * 根据id查询套餐
     * @param id
     */
    @Override
    public Setmeal findById(Integer id) {
        return setmealMapper.findById(id);
    }

    @Override
    public Integer[] findSetMealIdsByCheckGroupId(Integer id) {
        return setmealMapper.findSetMealIdsByCheckGroupId(id);
    }

    @Override
    public void edit(Integer[] checkgroupIds, Setmeal setmeal) {
        Setmeal byId = setmealMapper.findById(setmeal.getId());
        if(byId.getImg() != null){
            redisClient.srem(Result.DATA_TAG,byId.getImg());
        }
        setmealMapper.edit(setmeal);
        if(setmeal.getImg() != null){
            redisClient.sadd(Result.DATA_TAG,setmeal.getImg());
        }
        setmealMapper.deleteSetmealCheckgroup(setmeal.getId());
        if(checkgroupIds.length > 0){
            setmealMapper.addSetmealCheckgroup(checkgroupIds,setmeal.getId());
        }
    }
}




